import sys,string,math

nSets = 10;

def computeAveragesAndStandardErrors( inFile, outFile ):
	fIn = open(inFile,'r')
	fOut = open(outFile,'w')
	for line in fIn:
		val = line.split()
		x = float(val[0]);
		p2_1 = float(val[1]);
		p2_2 = float(val[2]);
		p2_3 = float(val[3]);
		p2_4 = float(val[4]);
		p2_5 = float(val[5]);
		p2_6 = float(val[6]);
		p2_7 = float(val[7]);
		p2_8 = float(val[8]);
		p2_9 = float(val[9]);
		p2_10 = float(val[10]);
		sumy = p2_1 + p2_2 + p2_3 + p2_4 + p2_5 + p2_6 + p2_7 + p2_8 + p2_9 + p2_10 
		avgy = float(sumy/nSets) 
		sqP2_1 = float((p2_1)**(2))
		sqP2_2 = float((p2_2)**(2))
		sqP2_3 = float((p2_3)**(2))
		sqP2_4 = float((p2_4)**(2))
		sqP2_5 = float((p2_5)**(2))
		sqP2_6 = float((p2_6)**(2))
		sqP2_7 = float((p2_7)**(2))
		sqP2_8 = float((p2_8)**(2))
		sqP2_9 = float((p2_9)**(2))
		sqP2_10 = float((p2_10)**(2))
		sum2 = float((sqP2_1+sqP2_2+sqP2_3+sqP2_4+sqP2_5+sqP2_6+sqP2_7+sqP2_8+sqP2_9+sqP2_10)/nSets)
		sumsq = float(((p2_1+p2_2+p2_3+p2_4+p2_5+p2_6+p2_7+p2_8+p2_9+p2_10)/nSets)**(2))
		sdy = float((sum2-sumsq)**0.5)
		sey = float(sdy/(nSets**0.5))
		print >>fOut, x,"\t",avgy,"\t",sey
	fIn.close()
	fOut.close()
		
# calculate averages and standard errors for p2x
# first step: collect all values in a single file p2xAll.dat
o = open('p2xAll.dat','w') 
f1 = open('analysisSet1/P2.dat','r')
f2 = open('analysisSet2/P2.dat','r')
f3 = open('analysisSet3/P2.dat','r')
f4 = open('analysisSet4/P2.dat','r')
f5 = open('analysisSet5/P2.dat','r')
f6 = open('analysisSet6/P2.dat','r')
f7 = open('analysisSet7/P2.dat','r')
f8 = open('analysisSet8/P2.dat','r')
f9 = open('analysisSet9/P2.dat','r')
f10 = open('analysisSet10/P2.dat','r')
while 1: # Create infinite loop
        line1 = f1.readline() # Read each line - data MUST begin at line 0.
        line2 = f2.readline()
	line3 = f3.readline()
        line4 = f4.readline()
        line5 = f5.readline()
        line6 = f6.readline()
        line7 = f7.readline()
        line8 = f8.readline()
        line9 = f9.readline()
        line10 = f10.readline()
	if not line1: break # Break reading loop if blank line
        if not line2: break
        if not line3: break
        if not line4: break
        if not line5: break
        if not line6: break
        if not line7: break
        if not line8: break
        if not line9: break
        if not line10: break
        words1 = string.split(line1) # Split each line into columns
        words2 = string.split(line2)
        words3 = string.split(line3)
        words4 = string.split(line4)
        words5 = string.split(line5)
        words6 = string.split(line6)
        words7 = string.split(line7)
        words8 = string.split(line8)
        words9 = string.split(line9)
        words10 = string.split(line10)
        print >> o, words1[0], words1[1], words2[1], words3[1], words4[1], words5[1], words6[1], words7[1], words8[1] , words9[1] , words10[1] # Print each value
f1.close()
f2.close()
f3.close()
f4.close()
f5.close()
f6.close()
f7.close()
f8.close()
f9.close()
f10.close()
o.close()
# second step: calculate actual avgs and SEs
computeAveragesAndStandardErrors( 'p2xAll.dat', 'avgP2x.dat' )

# calculate averages and standard errors for p2y
# first step: collect all values in a single file p2yAll.dat
o = open('p2yAll.dat','w') 
f1 = open('analysisSet1/P2.dat','r')
f2 = open('analysisSet2/P2.dat','r')
f3 = open('analysisSet3/P2.dat','r')
f4 = open('analysisSet4/P2.dat','r')
f5 = open('analysisSet5/P2.dat','r')
f6 = open('analysisSet6/P2.dat','r')
f7 = open('analysisSet7/P2.dat','r')
f8 = open('analysisSet8/P2.dat','r')
f9 = open('analysisSet9/P2.dat','r')
f10 = open('analysisSet10/P2.dat','r')
while 1: # Create infinite loop
        line1 = f1.readline() # Read each line - data MUST begin at line 0.
        line2 = f2.readline()
	line3 = f3.readline()
        line4 = f4.readline()
        line5 = f5.readline()
        line6 = f6.readline()
        line7 = f7.readline()
        line8 = f8.readline()
        line9 = f9.readline()
        line10 = f10.readline()
	if not line1: break # Break reading loop if blank line
        if not line2: break
        if not line3: break
        if not line4: break
        if not line5: break
        if not line6: break
        if not line7: break
        if not line8: break
        if not line9: break
	if not line10: break
        words1 = string.split(line1) # Split each line into columns
        words2 = string.split(line2)
        words3 = string.split(line3)
        words4 = string.split(line4)
        words5 = string.split(line5)
        words6 = string.split(line6)
        words7 = string.split(line7)
        words8 = string.split(line8)
        words9 = string.split(line9)
        words10 = string.split(line10)
        print >> o, words1[0], words1[2], words2[2], words3[2], words4[2], words5[2], words6[2], words7[2], words8[2] , words9[2] , words10[2] # Print each value
f1.close()
f2.close()
f3.close()
f4.close()
f5.close()
f6.close()
f7.close()
f8.close()
f9.close()
f10.close()
o.close()
# second step: calculate actual avgs and SEs
computeAveragesAndStandardErrors( 'p2yAll.dat', 'avgP2y.dat' )

# calculate averages and standard errors for p2z
# first step: collect all values in a single file p2zAll.dat
o = open('p2zAll.dat','w') 
f1 = open('analysisSet1/P2.dat','r')
f2 = open('analysisSet2/P2.dat','r')
f3 = open('analysisSet3/P2.dat','r')
f4 = open('analysisSet4/P2.dat','r')
f5 = open('analysisSet5/P2.dat','r')
f6 = open('analysisSet6/P2.dat','r')
f7 = open('analysisSet7/P2.dat','r')
f8 = open('analysisSet8/P2.dat','r')
f9 = open('analysisSet9/P2.dat','r')
f10 = open('analysisSet10/P2.dat','r')
while 1: # Create infinite loop
        line1 = f1.readline() # Read each line - data MUST begin at line 0.
        line2 = f2.readline()
	line3 = f3.readline()
        line4 = f4.readline()
        line5 = f5.readline()
        line6 = f6.readline()
        line7 = f7.readline()
        line8 = f8.readline()
        line9 = f9.readline()
        line10 = f10.readline()
	if not line1: break # Break reading loop if blank line
        if not line2: break
        if not line3: break
        if not line4: break
        if not line5: break
        if not line6: break
        if not line7: break
        if not line8: break
        if not line9: break
        if not line10: break
        words1 = string.split(line1) # Split each line into columns
        words2 = string.split(line2)
        words3 = string.split(line3)
        words4 = string.split(line4)
        words5 = string.split(line5)
        words6 = string.split(line6)
        words7 = string.split(line7)
        words8 = string.split(line8)
        words9 = string.split(line9)
        words10 = string.split(line10)
        print >> o, words1[0], words1[3], words2[3], words3[3], words4[3], words5[3], words6[3], words7[3], words8[3] , words9[3] , words10[3] # Print each value
f1.close()
f2.close()
f3.close()
f4.close()
f5.close()
f6.close()
f7.close()
f8.close()
f9.close()
f10.close()
o.close()
# second step: calculate actual avgs and SEs
computeAveragesAndStandardErrors( 'p2zAll.dat', 'avgP2z.dat' )
